<?php
/**
 * 快递控制器
 * User: ws
 * Date: 2018/8/31
 * Time: 16:11
 */

namespace app\admin\controller;
use app\admin\model\Shipping;
use app\admin\model\Order;
class Ship  extends Init
{

    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['name|shipping_code'] = array('like','%'.trim($key).'%');
            $list = Shipping::where($where)->order('shipping_id desc')->paginate(input('limit'))->toArray();
            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function add(){
        if(request()->isPost()){
            $data = $this->request->post();
            if (!Shipping::create($data)) {
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }

        return view('add');
    }

    function edit(){
        if(request()->isPost()){
            $data = $this->request->post();
            if (!Shipping::update($data)) {
                return json(array('code'=>0,'msg'=>'修改失败'));
            }
            return json(array('code'=>200,'msg'=>'修改成功'));
        }

        $list = Shipping::where('shipping_id', input('id'))->find()->toArray();
        return view('edit',['list'=>$list]);
    }


    function del(){
        $id = input('param.id');
        $data = Order::where('shipping_id',"$id")->find();
        if($data){
            return json(array('code'=>0,'msg'=>'删除失败，已经存在订单表'));
        }
        if (!Shipping::destroy(['shipping_id' => "$id"])) {
            return json(array('code'=>0,'msg'=>'删除失败'));
        }
        return json(array('code'=>200,'msg'=>'删除成功'));
    }

}